import { _decorator, Button, Color, Component, EventHandler, find, Label, misc, Node, Sprite, SpriteFrame } from 'cc';
import { ItemData } from '../Data';
import { NodeUtil } from '../AshFramework/Utils/NodeUtil';
import UI_Shaking from '../AshFramework/UI/UI_Shaking';
import { AudioManager, Audios } from '../AshFramework/Managers/AudioManager';
import { Panel, UIManager } from '../AshFramework/Managers/UIManager';
import { ResourceUtil } from '../AshFramework/Utils/ResourceUtil';
import { GameManager } from '../GameManager';
import Banner from '../Banner';

const { ccclass, property } = _decorator;

@ccclass('Item')
export class Item extends Component {
    Icon: Sprite = null;
    BG: Node;
    Selected: Node = null;
    Locked: Node = null;

    UpgrateButtonCan: Node;
    UpgrateButtonCant: Sprite;
    UpgrateButtonLabel: Label;
    UpgrateButtonCard: Node;
    FreeGetButton: Node;

    UpgrateButton: Node = null;
    ItemLv: Node = null;
    ItemLvQuality: Node = null;
    ItemLvLabel: Label = null;

    data: ItemData = null;

    cb: Function = null;

    onLoad(): void {
        this.Selected = find("Selected", this.node);
        this.Locked = find("Locked", this.node);
        this.Icon = find("Icon", this.node).getComponent(Sprite);
        this.BG = NodeUtil.GetNode("BG", this.node);
        this.UpgrateButton = NodeUtil.GetNode("UpgrateButton", this.node);
        this.ItemLv = NodeUtil.GetNode("ItemLv", this.node);
        this.UpgrateButtonCan = NodeUtil.GetNode("UpgrateButtonCan", this.node);
        this.UpgrateButtonCant = NodeUtil.GetComponent("UpgrateButtonCant", this.node, Sprite);
        this.UpgrateButtonLabel = NodeUtil.GetComponent("UpgrateButtonLabel", this.node, Label);
        this.UpgrateButtonCard = NodeUtil.GetNode("UpgrateButtonCard", this.node);
        this.FreeGetButton = NodeUtil.GetNode("FreeGetButton", this.node);
        this.ItemLvQuality = NodeUtil.GetNode("ItemLvQuality", this.node);
        this.ItemLvLabel = NodeUtil.GetComponent("ItemLvLabel", this.node, Label);
    }

    //普通卡牌
    Init(data: ItemData) {
        this.Reset();
        this.data = data;

        this.BG.children.forEach(e => {
            e.active = e.name == `${data.Quality}`;
            if (e.name == `${data.Quality}`) e.getComponent(Sprite).grayscale = !data.IsUnlock;
        });

        if (data.IsUnlock) {
            this.ItemLvQuality.children.forEach(e => e.active = e.name == `${data.Quality}`);
        }

        this.Refresh();

        ResourceUtil.LoadSpriteFrame(`Items/${data.Index}`).then((sp: SpriteFrame) => this.Icon.spriteFrame = sp);
    }

    //简单样式
    InitSimple(data: ItemData) {
        this.Init(data);
        this.node.getComponent(Button).enabled = false;
        this.UpgrateButton.active = false;
        this.Selected.active = false;
    }

    //点击卡牌有特殊回调
    InitWithCallback(data: ItemData, cb: Function) {
        this.Init(data);
        this.cb = cb;
    }

    //点击卡牌有特殊回调
    InitSimpleWithCallback(data: ItemData, cb: Function) {
        this.Init(data);
        this.UpgrateButton.active = false;
        this.Selected.active = false;
        this.cb = cb;
    }

    Refresh() {
        this.Icon.grayscale = !this.data.IsUnlock;
        this.Locked.active = !this.data.IsUnlock;
        this.ItemLv.active = this.data.IsUnlock;
        this.FreeGetButton.active = !this.data.IsUnlock;

        this.BG.children.forEach(e => {
            if (e.name == `${this.data.Quality}`) e.getComponent(Sprite).grayscale = !this.data.IsUnlock;
        })

        //已解锁
        if (this.data.IsUnlock) {
            this.ItemLvLabel.string = `Lv.${this.data.Lv}`;
            this.Selected.active = GameManager.Instance.IsEquipped(this.data.Index);

            if (this.data.IsMaxLv) {
                this.UpgrateButtonLabel.string = `满级`;
                this.UpgrateButtonCan.active = false;
                this.UpgrateButtonCant.node.active = true;
                this.UpgrateButtonCant.fillRange = 1;
            } else {
                let canUpgrate = this.data.Piece >= this.data.UpgratePiece;
                this.UpgrateButtonLabel.string = `${this.data.Piece}/${this.data.UpgratePiece}`;
                this.UpgrateButtonCan.active = canUpgrate;
                this.UpgrateButtonCant.fillRange = this.data.Piece / this.data.UpgratePiece;
                this.UpgrateButtonCant.node.active = !canUpgrate;
            }

            this.UpgrateButtonCard.active = !this.data.IsMaxLv;
        }

    }

    Reset() {
        this.cb = null;
        this.node.getComponent(Button).enabled = true;
    }

    OnButtonClick() {
        AudioManager.PlaySound(Audios.ButtonClick);

        if (this.cb) {
            this.cb(this.data);
            return;
        }

        if (this.data.IsUnlock) {
            UIManager.GetInstance().ShowPanel(Panel.ItemDetailPanel, [this.data]);
        } else {
            Banner.Instance.ShowVideoAd(() => {
                this.data.IsUnlock = true;
                this.Refresh();
            });
        }

    }
}